package com.qf.controller.market;

import com.alibaba.druid.util.StringUtils;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsAdminService;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller.market
 * @Description:
 * @Date 2022/10/18 17:50
 */
@RestController
@RequestMapping("/admin/brand")
public class AdminBrandController {

    @Autowired
    DtsBrandService brandService;
    @Autowired
    DtsAdminService adminService;
    @Autowired
    DtsCategoryService categoryService;

    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page ,
                       @RequestParam(value = "limit",required = false,defaultValue = "20") Integer limit,
                       @RequestParam(value = "sort",required = false,defaultValue = "add_time") String sort,
                       @RequestParam(value = "order",required = false,defaultValue = "desc") String order,
                       String name,Integer id){
//        admin/brand/list?page=1&limit=20&id=12&name=sss&sort=add_time&order=desc
        Map<String,Object> data = brandService.findBrandByPage(page,limit,sort,order,name,id);

        return ResponseUtil.ok(data);
    }

    @RequestMapping("/catAndAdmin")
    public Object lcatAndAdmin(){
        List<Map<String, Object>> adminList = adminService.findAll();
        List<CatVo> categoryList = categoryService.findCategoryList();
        Map<String, Object> data = new HashMap<>();
        data.put("adminList",adminList);
        data.put("categoryList",categoryList);

        return ResponseUtil.ok(data);
    }

    @PostMapping("/create")
    public Object create(@RequestBody DtsBrand brand){
        Object result = checkDtsBrand(brand);
        if (result != null){
            return result;
        }
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brand.setDeleted(false);
        brand.setShareUrl(brand.getPicUrl());

        try {
            brandService.insert(brand);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1,e.getMessage());
        }

        return ResponseUtil.ok(brand);
    }

    private Object checkDtsBrand(DtsBrand brand) {
        if (StringUtils.isEmpty(brand.getName()) || StringUtils.isEmpty(brand.getDesc())|| brand.getFloorPrice()==null){
            return ResponseUtil.fail(-1,"品牌制造商名称、编号和价格不能为空");
        }
        if (brand.getDefaultCategoryId()==null){
            return ResponseUtil.fail(-1,"主营类目不能为空");
        }
        if (brand.getAdminId()==null){
            return ResponseUtil.fail(-1,"管理员不能为空");
        }
        return null;
    }

    @PostMapping("/update")
    public Object update(@RequestBody DtsBrand brand){
        brand.setUpdateTime(new Date());
        brandService.update(brand);

        return ResponseUtil.ok(brand);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsBrand brand){
        brandService.delete(brand);
        return ResponseUtil.ok();
    }
}
